//
//  OrderListTableViewCell.m
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/20.
//

#import "OrderListTableViewCell.h"

@implementation OrderListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        self.backV = [[UIView alloc] init];
        [self.contentView addSubview:self.backV];
        self.backV.backgroundColor = [UIColor whiteColor];
        [self.backV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.top.equalTo(self.contentView.mas_top).offset(10);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        self.backV.layer.cornerRadius = 10;
        self.backV.layer.masksToBounds = YES;
        
    
        self.iconV = [[UIImageView alloc] init];
        [self.contentView addSubview:self.iconV];
        [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@70);
            make.right.equalTo(self.backV.mas_right);
            make.top.equalTo(self.contentView.mas_top);
            make.height.equalTo(@55);
        }];
        
        
        self.logoV = [[UIImageView alloc] init];
        [self.backV addSubview:self.logoV];
        [self.logoV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@21);
            make.left.equalTo(self.backV.mas_left).offset(20);
            make.top.equalTo(self.backV.mas_top).offset(15);
            make.height.equalTo(@21);
        }];
        
        self.nameLab = [UILabel createLabel:[UIFont fontWithName:kFuturaBold size:15] color:kBlackColor];
        [self.backV addSubview:self.nameLab];
        [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.logoV.mas_right).offset(5);
            make.right.equalTo(self.backV.mas_right).offset(-100);
            make.height.equalTo(@20);
            make.centerY.equalTo(self.logoV.mas_centerY);
        }];
        
        
        self.priceLab = [UILabel createLabel:[UIFont fontWithName:kFuturaMedium size:16] color:kButtonDefaultColor];
        [self.backV addSubview:self.priceLab];
        [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.logoV.mas_left);
            make.top.equalTo(self.iconV.mas_bottom).offset(10);
            make.right.equalTo(self.backV.mas_right).offset(-150);
            make.height.equalTo(@40);
        }];
        self.priceLab.numberOfLines = 2;
        
        self.dateLab = [UILabel createLabel:[UIFont fontWithName:kFuturaMedium size:14] color:kBlackColor];
        [self.backV addSubview:self.dateLab];
        [self.dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.backV.mas_right);
            make.height.equalTo(@40);
            make.left.equalTo(self.backV.mas_right).offset(-140);
            make.top.equalTo(self.priceLab.mas_top);
        }];
        self.dateLab.numberOfLines = 2;
        
       
        self.line = [[UIView alloc] init];
        [self.backV addSubview:self.line];
        self.line.backgroundColor = kTableViewBackColor;
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.logoV.mas_left);
            make.right.equalTo(self.backV.mas_right).offset(-20);
            make.height.equalTo(@1);
            make.top.equalTo(self.priceLab.mas_bottom).offset(15);
        }];
        
        self.line.hidden = YES;
        
        self.orderBtn = [[UIButton alloc] init];
        self.orderBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:15];
        [self.orderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.backV addSubview:self.orderBtn];
        [self.orderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@224);
            make.height.equalTo(@44);
            make.top.equalTo(self.line.mas_bottom).offset(15);
            make.left.equalTo(@((kScreenWidth - 40 - 224)/2));
        }];
        self.orderBtn.hidden = YES;
        self.orderBtn.layer.cornerRadius = 22;
        self.orderBtn.layer.masksToBounds = YES;
        
    
        
    
        

        self.statusLab = [UILabel createLabel:[UIFont fontWithName:kFutura size:13] color:kBlackColor];
        self.statusLab.textAlignment = 1;
        [self.contentView addSubview:self.statusLab];
        [self.statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@70);
            make.height.equalTo(@20);
            make.right.equalTo(self.backV.mas_right);
            make.top.equalTo(self.backV.mas_top).offset(-2);
        }];
        
        
        
    }
    return self;
}


- (void)setModel:(OrderListModel *)model{
    _model = model;
   
    [self.logoV sd_setImageWithURL:[NSURL URLWithString:esString(model.widespread)] placeholderImage:[UIImage imageNamed:@"icon_logo2"] options:SDWebImageRetryFailed];
    if (PMStringIsEmpty(model.widespread)) {
        self.logoV.image = [UIImage imageNamed:@"icon_Logo2"];
    }
    
    self.nameLab.text = esString(model.caulfeild);

    self.priceLab.text = [NSString stringWithFormat:@"%@\n%@",esString(model.de),esString(model.declined)];
    [self.priceLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:12]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#6C6C6C"]}] forStringArray:@[esString(model.declined),esString(model.declined)]];

    self.orderBtn.hidden = YES;
    self.line.hidden = YES;
 
    //Apply
    if ([model.understanding integerValue] == 120 || [model.understanding integerValue] == 0 ||[model.understanding integerValue] == 10) {
        self.dateLab.text = [NSString stringWithFormat:@"%@\n%@",esString(model.misunderstanding),esString(model.party)];
        [self.dateLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:12]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#6C6C6C"]}] forStringArray:@[esString(model.party),esString(model.party)]];
        
        self.line.hidden = NO;
        self.orderBtn.hidden = NO;
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateNormal];
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateSelected];
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateHighlighted];
        [self.orderBtn setTitle:ASLocalizedString(@"Apply") forState:UIControlStateNormal];
        [self.orderBtn setTitle:ASLocalizedString(@"Apply") forState:UIControlStateSelected];
        [self.orderBtn setTitle:ASLocalizedString(@"Apply") forState:UIControlStateHighlighted];
        self.orderBtn.backgroundColor = kButtonDefaultColor;
        [self.orderBtn setImgRightTitltLeft:10];
        [self.orderBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        [self.orderBtn setTitleColor:kWhiteColor forState:UIControlStateSelected];
        [self.orderBtn setTitleColor:kWhiteColor forState:UIControlStateHighlighted];
        
        self.iconV.image = [UIImage imageNamed:@"icon_orderApply"];
        
        self.statusLab.text = ASLocalizedString(@"Apply");
        self.statusLab.textColor = kWhiteColor;
    }
    //Delay
    else if ([model.understanding integerValue] == 180) {
        self.dateLab.text = [NSString stringWithFormat:@"%@\n%@",esString(model.drowning),esString(model.induced)];
        [self.dateLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:12]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#6C6C6C"]}] forStringArray:@[esString(model.induced),esString(model.induced)]];
        
        self.line.hidden = NO;
        self.orderBtn.hidden = NO;
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateNormal];
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateSelected];
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateHighlighted];
        [self.orderBtn setTitle:ASLocalizedString(@"Repayment") forState:UIControlStateNormal];
        [self.orderBtn setTitle:ASLocalizedString(@"Repayment") forState:UIControlStateSelected];
        [self.orderBtn setTitle:ASLocalizedString(@"Repayment") forState:UIControlStateHighlighted];
        self.orderBtn.backgroundColor = [UIColor colorWithHexString:@"#FF7171"];
        [self.orderBtn setImgRightTitltLeft:10];
        [self.orderBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [self.orderBtn setTitleColor:kBlackColor forState:UIControlStateSelected];
        [self.orderBtn setTitleColor:kBlackColor forState:UIControlStateHighlighted];
        
        self.iconV.image = [UIImage imageNamed:@"icon_orderDelay"];
        
        self.statusLab.text = ASLocalizedString(@"Delay");
        self.statusLab.textColor = kBlackColor;
    }
    //Repayment
    else if ([model.understanding integerValue] == 174) {
        self.dateLab.text = [NSString stringWithFormat:@"%@\n%@",esString(model.drowning),esString(model.induced)];
        [self.dateLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:12]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#6C6C6C"]}] forStringArray:@[esString(model.induced),esString(model.induced)]];
        
        self.line.hidden = NO;
        self.orderBtn.hidden = NO;
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateNormal];
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateSelected];
        [self.orderBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateHighlighted];
        [self.orderBtn setTitle:ASLocalizedString(@"Repayment") forState:UIControlStateNormal];
        [self.orderBtn setTitle:ASLocalizedString(@"Repayment") forState:UIControlStateSelected];
        [self.orderBtn setTitle:ASLocalizedString(@"Repayment") forState:UIControlStateHighlighted];
        self.orderBtn.backgroundColor = [UIColor colorWithHexString:@"#FAFF71"];
        [self.orderBtn setImgRightTitltLeft:10];
        [self.orderBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [self.orderBtn setTitleColor:kBlackColor forState:UIControlStateSelected];
        [self.orderBtn setTitleColor:kBlackColor forState:UIControlStateHighlighted];
        
        self.iconV.image = [UIImage imageNamed:@"icon_orderRepay"];
        
        self.statusLab.text = ASLocalizedString(@"Repayment");
        self.statusLab.textColor = kBlackColor;
        
    }
    //Finish
    else if ([model.understanding integerValue] == 200 ||[model.understanding integerValue] == 110) {
        self.dateLab.text = [NSString stringWithFormat:@"%@\n%@",esString(model.misunderstanding),esString(model.party)];
        [self.dateLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:12]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#6C6C6C"]}] forStringArray:@[esString(model.party),esString(model.party)]];
        
        self.iconV.image = [UIImage imageNamed:@"icon_orderFinish"];
        
        self.statusLab.text = ASLocalizedString(@"Finish");
        self.statusLab.textColor = kWhiteColor;
    }
    //Review
    else if ([model.understanding integerValue] == 21 || [model.understanding integerValue] == 151) {
        self.dateLab.text = [NSString stringWithFormat:@"%@\n%@",esString(model.misunderstanding),esString(model.party)];
        [self.dateLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:12]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#6C6C6C"]}] forStringArray:@[esString(model.party),esString(model.party)]];
        
        self.iconV.image = [UIImage imageNamed:@"icon_orderReview"];
        
        self.statusLab.text = ASLocalizedString(@"Review");
        self.statusLab.textColor = kBlackColor;
    }

    self.statusLab.transform = CGAffineTransformMakeRotation(-M_PI*15/180);
    
}

+ (CGFloat)getCellHeight:(OrderListModel *)mm{
    if ([mm.understanding integerValue] == 120 ||
        [mm.understanding integerValue] == 0 ||
        [mm.understanding integerValue] == 10 ||
        [mm.understanding integerValue] == 180 ||
        [mm.understanding integerValue] == 174
        ) {
        
        return 190;
    }
    return 120;

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
